﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LDLib
{
    /// <summary>
    /// Render batch. Handles the per-material batching
    /// of game objects.
    /// </summary>
    internal static class RenderBatch
    {
        private static Dictionary<Material, List<GameObject>> perMaterialEntities = new Dictionary<Material, List<GameObject>>();

        internal static void Add(Material material, GameObject go)
        {
            if (!perMaterialEntities.ContainsKey(material)) perMaterialEntities.Add(material, new List<GameObject>());
            perMaterialEntities[material].Add(go);
        }

        internal static void Render()
        {
            foreach(KeyValuePair<Material, List<GameObject>> pair in perMaterialEntities)
            {
                Material mat = pair.Key;
                mat.Set("view", Camera.Main.InverseView);
                mat.Set("projection", Camera.Main.projection);
                foreach(GameObject go in pair.Value)
                {
                    mat.Set("world", go.WorldTransform.Matrix);
                    mat.UseMaterial();
                    go.Mesh.RenderMesh();
                }
            }
        }
    }
}
